package com.atguigu.upp.service;

import com.atguigu.upp.beans.TagInfo;
import com.atguigu.upp.beans.TaskInfo;
import com.atguigu.upp.beans.TaskTagRule;
import com.atguigu.upp.mapper.TagInfoMapper;
import com.atguigu.upp.mapper.TaskInfoMapper;
import com.atguigu.upp.mapper.TaskTagRuleMapper;
import com.atguigu.upp.util.UPPUtil;
import lombok.extern.log4j.Log4j;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * Created by Smexy on 2023/3/24
 * <p>
 * 把访问某种数据源的所有的Dao(Mapper)的方法集成到一个Service服务中。
 * Dao:     只做一件事，读写数据库。
 * Service: 除了完成读写数据库(调用Dao)之外，还可以做一些其他的操作。
 * 例如： 参数校验
 * 日志记录
 * 对Dao查询的结果进行额外运算。
 */
@Log4j
public class MysqlService
{
    private TagInfoMapper tagInfoMapper;
    private TaskInfoMapper taskInfoMapper;
    private TaskTagRuleMapper taskTagRuleMapper;

    public MysqlService(SqlSession session) {
        tagInfoMapper = session.getMapper(TagInfoMapper.class);
        taskInfoMapper = session.getMapper(TaskInfoMapper.class);
        taskTagRuleMapper = session.getMapper(TaskTagRuleMapper.class);
    }

    public List<TagInfo> queryTags() {

        return tagInfoMapper.queryTagsNeedToExecute();

    }

    public List<TaskTagRule> queryRules(String taskId) {

        UPPUtil.validParams(taskId);

        List<TaskTagRule> rules = taskTagRuleMapper.queryRulesByTaskId(taskId);

        log.warn("rules:" + rules);

        return rules;

    }

    //根据taskId查询 taskInfo
    public TaskInfo getTaskInfo(String taskId) {

        //参数校验
        UPPUtil.validParams(taskId);

        TaskInfo taskInfo = taskInfoMapper.getTaskInfoById(taskId);

        //日志记录
        log.info("查询到了:" + taskId + "的信息: " + taskInfo);

        return taskInfo;
    }

    //根据taskId 查询 tagInfo
    public TagInfo getTagInfo(String taskId) {

        //参数校验
        UPPUtil.validParams(taskId);

        TagInfo tag = tagInfoMapper.getTagInfoByTaskId(taskId);

        //日志记录
        log.info("查询到了:" + taskId + "的对应的tag信息: " + tag);

        return tag;
    }


}
